<template>
	<view>
		<view class="address">
			<view class="addressCard" v-for="item in list" :key="item.id">
				<view class="aboutRec" @click="changeAddress(item)">
					<view class="recName">
						{{item.name}}
						<view class="isDefault" v-if="item.isCheck">默认</view>
					</view>
					<view class="recTel">{{item.phoneNumber}}</view>
				</view>
				<view class="addressEdit">
					<view class="addressEditinner">
						<image @click="goAddressEdit(item.id)" src="../../../static/icons/edit.png" mode=""></image>
					</view>
					
				</view>
			</view>
		</view>
		<!-- 新建地址 -->
		<view class="newAddress" @click="newAddress">
			新建地址
		</view>
	</view>
</template>

<script>
	import {findAllAddress} from "@/api/user/index.js"
	export default {
		data() {
			return {
				list:[],
				orderAddress:"",
				orderChecked:false,
			}
		},
		onLoad(type){
			// 判断是否传值从而判断是否由订单页面传值跳转而来
			this.orderAddress=type.type
			console.log(this.orderAddress)
			if(this.orderAddress=="select"){
				console.log("传值")
				this.orderChecked=true
			}else{
				console.log("无传值")
			}
			//获取地址列表
			findAllAddress().then(res=>{
				console.log(res)
				res.data.items.forEach(res=>{
					if(res.defaultStatus==1){
						res.isCheck=true
					}else{
						res.isCheck=false
					}
				})
				this.list=res.data.items
			})
		},
		methods: {
			// 更新地址
			goAddressEdit(val){
				uni.navigateTo({
					url:`./addressEdit/addressEdit?id=${val}`
				})
			},
			// 添加地址
			newAddress(){
				uni.navigateTo({
					url:"./addressEdit/addressEdit"
				})
			},
			//订单里地址的改变
			changeAddress(val){
				if(this.orderChecked){
					uni.setStorageSync("isAddress",val)
					uni.navigateBack({
						url:`../cart/order/order`
						
					})
					console.log(val)
					
				}
				
			}
		}
	}
	
</script>


<style lang="scss">
	page {
		background-color: #F1ECE7;
	}
</style>
<style lang="scss" scoped>
	//地址渲染
	.address{
		
		.addressCard{
			width: 94%;
			margin: 0 auto;
			padding: 10px 0;
			border-bottom: 1Px solid #ccc;
			display: flex;
			justify-content: space-between;
			.aboutRec{
				width: 300px;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				.recName{
					display: flex;
					font-size: 14px;
					margin-left: 15px;
					margin-bottom: 10px;
					.isDefault{
						font-size: 11px;
						background-color: #354e44;
						color: #fff;
						padding: 0 9px;
						border-radius: 4px;
						margin-left: 10px;
					}
				}
				.recTel{
					font-size: 12px;
					color: #999;
					margin-left: 15px;
				}
			}
			.addressEdit{
				width: 41px;
				height: 45px;
				display: flex;
				justify-content: center;
				align-items: center;
				.addressEditinner{
					width: 40px;
					height: 30px;
					display: flex;
					justify-content: center;
					align-items: center;
					border-left: solid 1px #aaa;
					image{
						width: 20px;
						height: 20px;
					}
				}
			}
		}
	}
	//新建地址
	.newAddress{
		position: fixed;
		bottom: 10px;
		left:50%;
		transform: translateX(-50%);
		box-shadow: 0px 2px 5px rgba(0,0,0,.4);
		width: 70%;
		height: 40px;
		border-radius: 40px;
		background-color: #354e44;
		color: #fff;
		text-align: center;
		line-height: 40px;
	}


</style>
